package utils;

import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Shayan
 */
public class Config {

    private static String REC_STORE = "CONFIGURATION";
    public final static String DEFAULT_SERVER_ADDRESS = "http://localhost:2780/";
    //"http://cctmdev2.londonmet.ac.uk/AntiObesity/";
    public final static String DEFAULT_DATABASE_STATE = "NONE";
    public final static String DEFAULT_REMEMBERED_USER_ID = "";
    public final static int SERVER_ADDRESS_KEY = 0;
    public final static int REMEMBERED_USER_ID_KEY = 1;
    public final static int DATABASE_STATE_KEY = 2;
    private static ConfigItem serverAddress;
    private static ConfigItem rememberedUserId;
    private static ConfigItem databaseState;

    static {
        serverAddress = new ConfigItem(SERVER_ADDRESS_KEY, DEFAULT_SERVER_ADDRESS);
        rememberedUserId = new ConfigItem(REMEMBERED_USER_ID_KEY, DEFAULT_REMEMBERED_USER_ID);
        databaseState = new ConfigItem(DATABASE_STATE_KEY, DEFAULT_DATABASE_STATE);
    }

    public static String getDatabaseState() throws RecordStoreException, IOException {
        reload();
        return Config.databaseState.getValue();
    }

    public static void setDatabaseState(String databaseState) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        Config.databaseState.setValue(databaseState);
        save();
    }

    public static String getServerAddress() {
        try {
            reload();
        } catch (Exception ex) {
            return DEFAULT_SERVER_ADDRESS;
        }
        return Config.serverAddress.getValue();
    }

    public static void setServerAddress(String serverAddress) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        Config.serverAddress.setValue(serverAddress + (serverAddress.endsWith("/") ? "" : "/"));
        save();
    }

    public static String getRememberedUserId() {
        try {
            reload();
            return Config.rememberedUserId.getValue();
        } catch (Exception ex) {
            return DEFAULT_REMEMBERED_USER_ID;
        }
    }

    public static void setRememberedUserId(String id) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        Config.rememberedUserId.setValue(id);
        save();
    }

    private static void reload() throws RecordStoreException, IOException {
        RecordStore rs = RecordStore.openRecordStore(REC_STORE, true);
        RecordEnumeration re = rs.enumerateRecords(null, null, false);
        while (re.hasNextElement()) {
            byte[] data = re.nextRecord();
            int key = (data[2] << 8) | data[3];
            switch (key) {
                case SERVER_ADDRESS_KEY:
                    Config.serverAddress = ConfigItem.create(data);
                    break;
                case REMEMBERED_USER_ID_KEY:
                    Config.rememberedUserId = ConfigItem.create(data);
                    break;
                case DATABASE_STATE_KEY:
                    Config.databaseState = ConfigItem.create(data);
                    break;
            }
        }
        rs.closeRecordStore();
    }

    private static void save() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        try {
            RecordStore.deleteRecordStore(REC_STORE);
        } catch (RecordStoreNotFoundException ex) {
            // silently discard
        }
        RecordStore rs = RecordStore.openRecordStore(REC_STORE, true);
        storeItem(rs, Config.serverAddress);
        storeItem(rs, Config.rememberedUserId);
        storeItem(rs, Config.databaseState);

        rs.closeRecordStore();
    }

    private static void storeItem(RecordStore rs, ConfigItem item) throws RecordStoreNotOpenException, RecordStoreException, IOException {
        byte[] rec = item.getData();
        rs.addRecord(rec, 0, rec.length);
    }
}
